# coding:utf-8
# data = [{'name': '组织部门', 'url': '/dept', 'id': 3, 'pid': 1, 'pname': '组织管理'}, {'name': '角色管理', 'url': '/role', 'id': 5, 'pid': 2, 'pname': '菜单权限'}, {'name': '用户管理', 'url': '/user', 'id': 4, 'pid': 1, 'pname': '组织管理'}]

def xdata(data):
    lst = []
    idlist = []
    for i in data:
        if i["pid"] not in idlist:
            lst.append({"id":i["pid"],"name":i["pname"],"child":[]})
            idlist.append(i["pid"])

    for key,val in enumerate(lst):
        for i in data:
            if val["id"]==i["pid"]:
                lst[key]["child"].append({"id":i["id"],"name":i["name"],"url":i["url"]})
        # dic = {}
        # if i["pid"]:
        #     dic["name"] = i["pname"]
        #     dic["id"] = i["pid"]
        #     # dic["child"] = [{"id": i["id"], "name": i["name"], "url": i["url"]}]
        #     # dic["child"] = []
        #     if len(list) != 0:
        #         for j in list:
        #             if j["id"]!=i["pid"]:
        #                 list.append(dic)
        #     else:
        #         list.append(dic)

        # for j in list:
        #
        #     list.append(dic)
        #
        #     for j,k in enumerate(dic["child"]):
        #         if k["id"] == i["pid"]:
                # if i["pid"] == j["id"]:
                #     dic["child"].append({"id":i["id"],"name":i["name"],"url":i["url"]})
                #     pass
                # else:
                #     dic["child"] = [{"id":i["id"],"name":i["name"],"url":i["url"]}]
                #     list.append(dic)

    return lst
